<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日期计算器</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f3f4f6;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #ffffff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            border-radius: 8px;
            width: 300px;
            text-align: center;
        }

        h1 {
            color: #333;
            font-size: 24px;
            margin-bottom: 20px;
        }

        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            width: 100%;
            margin-top: 10px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>日期计算器</h1>
        <p>请输入一个日期和要加上的天数：</p>

        <input type="date" id="inputDate" placeholder="选择日期">
        <input type="number" id="inputDays" placeholder="天数（正数或负数）">

        <button onclick="calculateDate()">计算新日期</button>

        <div id="result" class="result"></div>
    </div>

    <script>
        // updateDateByDay 函数
        const updateDateByDay = (date, n) => {
            date = new Date(date);
            date.setDate(date.getDate() + n);
            return date.toISOString().split('T')[0];
        };

        // 计算新日期的函数
        function calculateDate() {
            const inputDate = document.getElementById('inputDate').value;
            const inputDays = parseInt(document.getElementById('inputDays').value);

            // 判断输入是否合法
            if (!inputDate || isNaN(inputDays)) {
                document.getElementById('result').textContent = '请输入有效的日期和天数';
                return;
            }

            // 调用 updateDateByDay 函数
            const newDate = updateDateByDay(inputDate, inputDays);
            document.getElementById('result').textContent = `新日期是：${newDate}`;
        }
    </script>

</body>

</html>